<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <com-a></com-a>
      <com-b></com-b>
    </div>

    <template id="tmpa">
      <div>
        <h2>comA</h2>
        <h2>{{msg}}</h2>
        <div>接受数据: {{data}}</div>
        <button @click="send">click</button>
      </div>
    </template>
    <template id="tmpb">
      <div>
        <h2>comB</h2>
        <h2>{{msg}}</h2>
        <div>接受数据: {{data}}</div>
        <button @click="send">click</button>
      </div>
    </template>

    <script src="./vue.js"></script>
    <script>
      var eventBus = new Vue();
      
      const comA = {
        data() {
          return {
            msg: "comA 的数据",
            data: "",
          };
        },
        methods: {
          send() {
            eventBus.$emit("tobusA", this.msg);
          },
        },
        mounted() {
          eventBus.$on("tobusB", (res) => {
            this.data = res;
          });
        },
        template: "#tmpa",
      };

      const comB = {
        data() {
          return {
            msg: "comB 的数据",
            data: "",
          };
        },
        methods: {
          send() {
            eventBus.$emit("tobusB", this.msg);
          },
        },
        mounted() {
          eventBus.$on("tobusA", (res) => {
            this.data = res;
          });
        },
        template: "#tmpb",
      };

      var vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components: {
          comA,
          comB,
        },
      });
    </script>
  </body>
</html>
